//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.KyComboCode;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class KyComboCodeDao extends BaseDao<KyComboCode, String> {
    public KyComboCodeDao() {
    }

    public List<KyComboCode> getShopComboList(String shopCode) {
        String sql = "select code, chName, chargeAmount, presentAmount, valids, validUnit from KyComboCode ic inner join KyComboShop s on ic.code=s.comboCode\nwhere shopCode=:p1 and status=1 and ActiveTime<getdate() and InactiveTime>getdate()";
        return this.queryListBySql(KyComboCode.class, sql, new Object[]{shopCode});
    }

    public List<DicShopCode> getKyComboShopList(String code, boolean ball) {
        String sql;
        if(ball) {
            sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status from DicShopCode s\nleft join KyComboShop a on s.code=a.shopCode and comboCode = :p1\nwhere status>0 order by s.code";
        } else {
            sql = "select s.code, s.chName, status from DicShopCode s\ninner join KyComboShop a on s.code=a.shopCode\nwhere comboCode = :p1 and status>0 order by s.code";
        }

        return this.queryListBySql(DicShopCode.class, sql, new Object[]{code});
    }

    public void addKyCombo(KyComboCode kyCombo, List<String> shopList) {
        this.add(kyCombo);
        this.saveShop(kyCombo.getCode(), shopList);
    }

    public void updateKyCombo(KyComboCode kyCombo, List<String> shopList) {
        this.update(kyCombo);
        this.saveShop(kyCombo.getCode(), shopList);
    }

    private void saveShop(String comboCode, List<String> shopList) {
        Session session = this.getSession();
        SQLQuery query = session.createSQLQuery("delete from KyComboShop where comboCode = :p1");
        query.setString("p1", comboCode);
        query.executeUpdate();
        query = session.createSQLQuery("insert into KyComboShop (comboCode, ShopCode) values (:p1, :p2)");
        query.setString("p1", comboCode);
        Iterator i$ = shopList.iterator();

        while(i$.hasNext()) {
            String shopCode = (String)i$.next();
            query.setString("p2", shopCode);
            query.executeUpdate();
        }

    }

    public boolean updateStatus(String code, int status) {
        String hql = "update KyComboCode set status=:p1 where code=:p2";
        return this.updateByHql(hql, new Object[]{Integer.valueOf(status), code}) > 0;
    }
}
